package org.jeecg.modules.columnTopic.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.MQConstant;
import org.jeecg.common.mq.config.ActiveMQBeanConfig;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.constant.ConsumerConstant;
import org.jeecg.constant.RedisConstant;
import org.jeecg.modules.columnTopic.entity.ColumnTopicPublish;
import org.jeecg.modules.columnTopic.service.IColumnTopicPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 栏目订阅
 * @Author: jeecg-boot
 * @Date:   2020-12-05
 * @Version: V1.0
 */
@Api(tags="栏目订阅")
@RestController
@RequestMapping("/columnTopic")
@Slf4j
public class ColumnTopicPublishController extends JeecgController<ColumnTopicPublish, IColumnTopicPublishService> {
	@Autowired
	private IColumnTopicPublishService columnTopicPublishService;
	@Autowired
	private ActiveMQBeanConfig activeMQBeanConfig;
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private ISysBaseAPI iSysBaseAPI;
	/**
	 *   添加
	 *
	 * @param columnTopicPublish
	 * @return
	 */
	@AutoLog(value = "栏目订阅-添加")
	@ApiOperation(value="栏目订阅-添加", notes="栏目订阅-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ColumnTopicPublish columnTopicPublish) {
		String columnName = columnTopicPublish.getColumnName();
		if (StringUtils.isNotEmpty(columnName)){
				// 不为空，通过名称查询对应的栏目id
				String id = columnTopicPublishService.getColumnIdByName(columnName);
				columnTopicPublish.setColumnId(id);
		}
		// 异步栏目订阅修改redis
		Map<String, Object> map = new HashMap<>(2);
		map.put(MQConstant.OPERATION, ConsumerConstant.PHIL_INSERT_TOPIC_TO_REDIS);
		String jsonStr = JSON.toJSONString(columnTopicPublish);
		map.put(MQConstant.INFO,jsonStr);

		jmsMessagingTemplate.convertAndSend(activeMQBeanConfig.queueTopicRedis(),map);
		// 操作数据库
		columnTopicPublishService.save(columnTopicPublish);
		return Result.ok("添加成功！");
	}

	/**
	 *   通过id删除
	 *
	 * @param columnTopicPublish
	 * @return
	 */
	@AutoLog(value = "栏目订阅-通过openId,栏目id删除")
	@ApiOperation(value="栏目订阅-通过openId,栏目id删除", notes="栏目订阅-通过openId,栏目id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestBody ColumnTopicPublish columnTopicPublish) {
		String columnName = columnTopicPublish.getColumnName();
		if (StringUtils.isNotBlank(columnName)){
			// 不为空，通过名称查询对应的栏目id
			String id = columnTopicPublishService.getColumnIdByName(columnName);
			columnTopicPublish.setColumnId(id);
		}
		// 同步删除
		columnTopicPublishService.remove(new LambdaQueryWrapper<ColumnTopicPublish>().
				eq(ColumnTopicPublish::getColumnId,columnTopicPublish.getColumnId()).
				eq(ColumnTopicPublish::getOpenId,columnTopicPublish.getOpenId()));
		// 异步删除redis
		Map<String, Object> map = new HashMap<>(2);
		map.put(MQConstant.OPERATION, ConsumerConstant.PHIL_DELETE_TOPIC_FROM_REDIS);
		map.put(MQConstant.INFO,JSON.toJSONString(columnTopicPublish));
		jmsMessagingTemplate.convertAndSend(activeMQBeanConfig.queueTopicRedis(),map);
		return Result.ok("删除成功!");
	}


	 /**
	  *   通过栏目id和openId查询状态
	  *
	  * @param columnTopicPublish
	  * @return
	  */
	 @AutoLog(value = "栏目订阅-查询状态")
	 @ApiOperation(value="栏目订阅-查询状态", notes="栏目订阅-查询状态")
	 @PostMapping(value = "/getStatus")
	 public Result<?> getStatus(@RequestBody ColumnTopicPublish columnTopicPublish) {
		 String columnName = columnTopicPublish.getColumnName();
		 if (StringUtils.isNotBlank(columnName)){
			 String id = columnTopicPublishService.getColumnIdByName(columnName);
			 columnTopicPublish.setColumnId(id);
		 }
		 // 查找是否订阅了对应的栏目
		 HashMap<String, Object>map = (HashMap<String, Object>) redisUtil.get(RedisConstant.ARTICLE_PUBLISH+columnTopicPublish.getColumnId());
		 Object o = map.get(columnTopicPublish.getOpenId());
		 return Result.OK(o!=null);
	 }

	 /**
	  *   通过penId获取该用户订阅的所有栏目的一些信息
	  *
	  * @param openId 用户openId
	  * @return
	  */
	 @AutoLog(value = "栏目订阅-获取订阅栏目新推信息")
	 @ApiOperation(value="栏目订阅-获取订阅栏目新推信息", notes="栏目订阅-获取订阅栏目新推信息")
	 @GetMapping(value = "/getTopicInfo")
	 public Result<?> getTopicInfo(@RequestParam(value = "openId") String openId) {
		 return Result.OK(columnTopicPublishService.getTopicInfo(openId));
	 }

	/**
	 *   通过栏目id和openId获取当前栏目的更新数量
	 *
	 * @return
	 */
	@AutoLog(value = "栏目订阅-测试")
	@ApiOperation(value="栏目订阅-测试", notes="栏目订阅-测试")
	@GetMapping(value = "/test")
	public Result<?> tset() {
//		redisUtil.set(RedisConstant.ARTICLE_PUBLISH+"1332568345099620353",new HashMap<String, Object>());
//		for (int i = 0; i < 50; i++) {
//			jmsMessagingTemplate.convertAndSend(activeMQBeanConfig.test(),String.valueOf(i));
//		}
		List<DictModel> column_manage = iSysBaseAPI.queryDictItemsByCode("column_manage");
		return Result.OK(column_manage);
	}
}
